<?php

namespace Device\Controller;

/**
 * 设备
 * @auth zuly
 * @date 4/25/21
 */
class Info extends \App\Application {

	private $_deviceService;

	/**
	 * 构造函数
	 */
	public function __construct($appName, $controllerName, $actionName) {
		parent::__construct($appName, $controllerName, $actionName);
		$this->_deviceService = \Lsf\Loader::service('Device', FALSE, APP_NAME_DEVICE);
	}

	/**
	 * 设备配置信息获取
	 */
	public function getDevice() {
		$params = $this->post('', TRUE);
		if (!isset($params['device_id']) || empty($params['device_id'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'device_id');
		}
		$result = $this->_deviceService->getDeviceConfig($params['device_id']);
		if(!is_array($result) || empty($result)){
			return $this->json(4,[],'获取设备配置信息失败');
		}
		return $this->json(ECODE_SUCCESS, $result);
	}

	/**
	 * 设备配置
	 */
	public function setDevice() {
		$params = $this->post('', TRUE);
		if (!isset($params['device_id']) || empty($params['device_id'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'device_id');
		}
		if (!isset($params['is_admin'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'is_admin');
		}
		if (!isset($params['group_id']) || empty($params['group_id'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'group_id');
		}
		if($params['is_admin']){
			$params['group_id'] = 3;
		}
		$result = $this->_deviceService->setDeviceConfig($params['device_id'], $params['group_id']);
		if(!is_array($result) || empty($result)){
			return $this->json(4,[],'获取设备配置信息失败');
		}
		return $this->json(ECODE_SUCCESS, $result);
	}

	/**
	 * 获取七牛凭证
	 */
	public function getQiNiuToken(){
		$result = $this->_deviceService->getQiNiuToken();
		if(!is_array($result) || empty($result)){
			return $this->json(4,[],'获取七牛token失败');
		}
		return $this->json(ECODE_SUCCESS, $result);
	}
}
